package common

import (
	"github.com/mojocn/base64Captcha"
	"image/color"
	"mycms/global"
)

var store = base64Captcha.DefaultMemStore

//	创建验证码
func GetCaptcha() (id, b64s string, err error) {

	var driver base64Captcha.Driver
	var driverString base64Captcha.DriverString

	// 配置验证码信息
	captchaConfig := base64Captcha.DriverString{
		Height:          global.CONFIG.Captcha.ImgHeight,
		Width:           global.CONFIG.Captcha.ImgWidth,
		NoiseCount:      global.CONFIG.Captcha.NoiseCount,
		ShowLineOptions: 2 | 4,
		Length:          global.CONFIG.Captcha.KeyLong,
		Source:          global.CONFIG.Captcha.Source,
		BgColor: &color.RGBA{
			R: 217,
			G: 217,
			B: 217,
			A: 217,
		},
		//Fonts: []string{"wqy-microhei.ttc"},	//	验证码字体
	}

	driverString = captchaConfig
	driver = driverString.ConvertFonts()
	captcha := base64Captcha.NewCaptcha(driver, store)
	lid, lb64s, lerr := captcha.Generate()
	return lid, lb64s, lerr
}

//	比对验证码
func VerifyCaptcha(id string,capt string) bool {
	if store.Verify(id, capt, false) {
		return true
	} else {
		return false
	}
}
